package org.zjt.demo

/**
  * DESC    List一但创建，其值不能被改变 （只不能更新）
  *
  * @author
  **/
object TestList {
  def main(args: Array[String]): Unit = {
    var list = List("a", "b", "c", 'd')
    list.foreach(print)


    println()
    list = List.apply("a", "b", "c", 'd', 'e')
    list.foreach(print)

    println()
    val diagMatrix = List(List(2, 34, 3, 2, 34), List(2, 54, 3, 1, 3, 5))
    diagMatrix.foreach(a => a.foreach(print))

    var datalist = List(1, 23, 43, 2, 34, 2, 1)
    for (i <- 0 until datalist.length)
      println(datalist.updated(i, datalist(i) + i)) //返回一个新的list
    datalist.foreach(print)

    /*
      error:编译错误
      for (i <- 0 until datalist.length)
        datalist(i) = datalist(i) + i
      datalist.foreach(println)
      */

    println(" :: / ++ 都是追加元素或者list    Nil 也代表了空列表")
    val fruit = "apples" :: ("oranges" :: "li" :: ("pears" :: "zhang" :: Nil))
    println(if (fruit.isEmpty) Nil else fruit.mkString(","))


    val nums = 1 :: (2 :: (3 :: (4 :: Nil)))
    println(if (nums.isEmpty) Nil else nums.mkString(","))

  }

}
